package com.dtu.XMLExtras;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.dtu.ImagesExtras.ImageAndText;


public class ReadXMLImages extends DefaultHandler{

	// ===========================================================
	// Fields
	// ===========================================================
	private ArrayList<ImageAndText> myImages = new ArrayList<ImageAndText>();
	private String XMLpath = "";

	public ReadXMLImages(){
	}
	
	public ReadXMLImages(String path){
		XMLpath = path;
	}

	public void setPath(String path){
		XMLpath = path;
	}
	
	public ArrayList<ImageAndText> getParsedData() {
		return this.myImages;
	}

	// ===========================================================
	// Methods
	// ===========================================================
	@Override
	public void startDocument() throws SAXException {
		this.myImages = new ArrayList<ImageAndText>();
	}

	@Override
	public void endDocument() throws SAXException {
		// Nothing to do
	}

	/** Gets be called on opening tags like:
	 * <tag>
	 * Can provide attribute(s), when xml was like:
	 * <tag attribute="attributeValue">*/
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		if (localName.equals("Image")) {
			String id = atts.getValue("ID");
			String path = atts.getValue("Thumbnail");
			String owner = atts.getValue("Owner");
			String location = atts.getValue("Location");
			myImages.add(new ImageAndText(id, owner + " uploaded a photo @ " + location));
		}
	}

	public ArrayList<ImageAndText> getImages(){
		ArrayList<ImageAndText> thumbnails = new ArrayList<ImageAndText>();
		try {

			/* Create a URL we want to load some xml-data from. */
			URL url = new URL(XMLpath);

			/* Get a SAXParser from the SAXPArserFactory. */
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();

			/* Get the XMLReader of the SAXParser we created. */
			XMLReader xr = sp.getXMLReader();
			/* Create a new ContentHandler and apply it to the XML-Reader*/
			ReadXMLImages myXMLHandler = new ReadXMLImages();
			xr.setContentHandler(myXMLHandler);

			/* Parse the xml-data from our URL. */
			xr.parse(new InputSource(url.openStream()));
			/* Parsing has finished. */
			thumbnails = myXMLHandler.getParsedData();
			return thumbnails;

		} catch (Exception e) {
		}
		return null;

		/* Our ExampleHandler now provides the parsed data to us. */
		
	}
	
	 public Bitmap getRemoteImage(final URL aURL) {
		 
		 
         try {
                 final URLConnection conn = aURL.openConnection();
                 conn.connect();
                 final BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
                 final Bitmap bm = BitmapFactory.decodeStream(bis);
                 bis.close();
                 return bm;

         } catch (IOException e) {
         }
         return null;

 }


}